﻿using UnityEngine;
using System.Collections;

public class Popup_WriteMessage : Popup_Base
{
	[HideInInspector] public long receiverId = -1;
	[HideInInspector] public string receiverName = string.Empty;

	string initialReceiverNameLabelText;
	string initialMessageLabelText;

	public UIInput receiverNameInput;
	public UIInput messageInput;

	public override void AwakeInit ()
	{
		base.AwakeInit ();
		initialReceiverNameLabelText = receiverNameInput.label.text = Localization.Get("uiMailboxInsertName");
		initialMessageLabelText = messageInput.label.text = Localization.Get("uiMailboxInsertMessage");
	}

	public void OnClick_CancelButton()
	{
		if (string.Compare(messageInput.label.text.Trim(), initialMessageLabelText) != 0 && messageInput.label.text.Trim().Length > 0)
		{
			Popup_TwoButton pt = PopupManager.PushPopup(PopupKind.TwoButton) as Popup_TwoButton;
			pt.SetPopupWithTitleAndBody(Localization.Get("uiCommonTextwarning"), Localization.Get("uiPopupMailboxCaution2"));
			pt.SetPopupWithYesNoString(Localization.Get("uiCommonTextCheck"), Localization.Get("uiCommonTextCancel"));
			pt.SetEventOK(GameObject.Find("SceneManager"), "OnClick_CloseAll");
		}
		else
		{
			BackButton();
		}
	}

	public void OnClick_ConfirmButton()
	{
		EPLog.Log("OnClick_ConfirmButton");

		// 받는이 이름이 비어있을 경우 (초기값이 입력되어 있음)
		if (string.Compare(receiverNameInput.value.Trim(), initialReceiverNameLabelText) == 0)
		{
			PopupUtility.OneButtonErrorPopupNoBackground(Localization.Get("uiPopupMailboxCaution5"));
			return;
		}

		// 메시지의 내용이 비어있음
		if (string.Compare(messageInput.value.Trim(), initialMessageLabelText) == 0 || messageInput.value.Trim().Length <= 0)
		{
			PopupUtility.OneButtonErrorPopupNoBackground(Localization.Get("uiPopupMailboxCaution5"));
			return;
		}

		OnClick_ConfirmPostProcess();
	}
	
	public void OnClick_ConfirmPostProcess()
	{
		EPLog.Log("OnClick_ConfirmPostProcess");
		NetworkManager.Instance.C2S_MSG_SMAIL_BY_NAME(receiverNameInput.value.Trim(), UnityEngine.WWW.EscapeURL(messageInput.value.Trim()), (responseData) => {
			
			if (responseData.ecode != 0)
			{
				PopupUtility.OneButtonErrorPopupNoBackground(responseData.ecode);
				return;
			}
			else
			{
				Popup_OneButton po = PopupManager.PushPopup(PopupKind.OneButton) as Popup_OneButton;
				po.SetPopupWithTitleAndBody(Localization.Get("uiCommonTextwarning"), Localization.Get("uiPopupMailboxCaution7"));
				// TODO: Need to change Delegate Method
				po.SetEventOK(GameObject.Find("SceneManager"), "OnClick_CloseAll");
			}
		});
	}

	public void OnClick_FindFriend()
	{
		Popup_SelectFriend pf = PopupManager.PushPopup(PopupKind.SelectFriend) as Popup_SelectFriend;
		pf.sceneObject = this.gameObject;
	}

}
